<!doctype html>
<html lang="en" style="background:#fff;">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" href="../../css/aui.css">
    <link rel="stylesheet" href="../../css/orderAndPay.css">
    <link rel="stylesheet" href="../../css/pulice.css">
    <link rel="stylesheet" href="../../css/base.css">
    <style>
        <!-- ！TOdo 订单页面-->
        .ui-loader-default{ display:none}
        .ui-mobile-viewport{ border:none;}
        .ui-page {padding: 0; margin: 0; outline: 0; min-height: auto !important;}


        .aui-radio:checked, .aui-radio.aui-checked, .aui-checkbox:checked, .aui-checkbox.aui-checked{
            background-color: #ef0b21;
            border: solid 1px #ef0b21;
        }
        .aui-radio, .aui-checkbox{
            width: .9rem;
            height: .9rem!important;
        }

        .aui-radio:checked {
            background-color: #ef0b21;
            border: solid 1px #ef0b21;
            text-align: center;
            background-clip: padding-box;
        }
    </style>
    <script src="../../js/api.js"></script>
    <script src="../../js/libs/zepto.min.js"></script>
    <script>
        $(function () {
            var money = [];
            var sum1 = 0;
            var sum2 = 0;
            var $shopArr = $('.order-shop');
            $.each($shopArr, function (i,value) {
                var $shopItem = $shopArr.eq(i).find('.order-goods-item');
                $.each($shopItem, function (b) {
                    money.push($shopItem.eq(b).find('.c-r').find('span').text() * $shopItem.eq(b).find('.order-goods-number').find('span').text());
                });
                $.each(money, function (c) {
                    sum1 = money[c] + money[c];
                });
                sum1 += parseFloat($('.yf').text());
                var reg = /^[0-9]*[1-9][0-9]*$/;
                if (reg.test(sum1)) {
                    $shopArr.find('.order-shop-config > :last-child').find(".c-r").html('' +
                        '<span class="c-r">&nbsp;&yen;&nbsp;'+parseInt(sum1)+'.<span class="money1">'+ '00' +'</span></span>'
                    );
                }else {
                    var xs = sum1.split('.');
                    var x = xs[1];
                    $shopArr.find('.order-shop-config > :last-child').find(".c-r").html('' +
                        '<span class="c-r">&nbsp;&yen;&nbsp;'+parseInt(sum1)+'.<span class="money1">'+ x +'</span></span>'
                    );
                }
                sum2 += sum1;
            });
            $('.order-foot').find('.c-r').eq(0).text();

            //引入头部
            $('.hm-bar').load('../../data/headerOther.html',function () {
                $(this).find('.head-main').text('确认订单');
                $(this).find('.head-left').on('tap',function () {
                    window.history.back();
                })
            });
        })
    </script>
    <title>确认订单</title>
</head>

<body style="background:#fff;">
<div class="hm-bar"></div>
<div id="app">

    <div class="order-info pd-lf font-base-22">
        <ul class="order-info-name">
            <li>收货人：<span>{{orderView.address.userName}}</span></li>
            <li>{{orderView.address.userPhone}}</li>
        </ul>
        <div class="order-info-address">
            <img src="../../img/orderDw.png" alt=""/>
            <span>{{orderView.address.name+''+orderView.address.userAddress}}</span>
            <img src="../../img/goIcon.png" alt=""/>
        </div>
    </div>
    <div class="order-shop-box">
        <div class="order-shop" v-for="o in orderView.goodsList ">
            <div class="order-shop-name font-base-24 pd-lf">
                <img src="../../img/orderShop.png" alt=""/>
                <span>{{o.catName}}</span>
                <img src="../../img/goIcon.png" alt=""/>
            </div>
            <div class="order-shop-goods font-base-22 pd-lf" >
                <div class="order-goods-item" v-for="(p,index) in o.list">
                    <img :src="orImg(p.goodsImg)" alt=""/>
                    <ul class="order-goods-info">
                        <li class="order-goods-name">
                            <span style="line-height: 1.5">{{p.goodsName}}</span>
                            <span class="c-r">&yen;<span>{{p.specPrice}}</span></span>
                        </li>
                        <li class="order-goods-type">
                            <span>颜色：黑色&nbsp;&nbsp;&nbsp;&nbsp;版本:32G</span>
                        </li>
                        <li class="order-goods-number">
                            &times;<span>{{p.cartNum}}</span>
                        </li>
                    </ul>
                </div>

            </div>
            <div class="order-shop-config font-base-28">
                <div>
                    此商品不支持7天无理由退换货
                </div>
                <ul>
                    <li>配送方式</li>
                    <li><span class="c-r">运费:&nbsp;&yen;&nbsp;<span class="yf">{{returnFloat(orderView.allAddressPrice)}}</span></span></li>
                </ul>
                <div>
                    <span>买家留言：</span><input type="text" placeholder="选填：填写内容已和卖家协商确认" style="flex: 1;" v-model="o.orderRemarks">
                </div>
                <div>
                    共{{o.num}}件商品&nbsp;&nbsp;&nbsp;&nbsp;小计:<span class="c-r">&nbsp;&yen;&nbsp;{{splitPrice(o.minMoney)[0]}}.<span class="money1">{{splitPrice(o.minMoney)[1]}}</span></span>
                </div>
            </div>
        </div>
        <!-- 发票信息栏-->
        <div class="order-shop-config font-base-28" >
                <div>
                    <span class="aui-font-size-14"> 是否需要发票：</span>
                    <div style="margin-left: 4rem">
                        <label style="padding: 0 1rem" for="yes">   <input class="aui-radio" type="radio" name="demo1"  v-model="isInvoice" value="1" id="yes" checked >  是</label>

                        <label for="no"> <input class="aui-radio" type="radio" name="demo1"  v-model="isInvoice" id="no" value="0" > 否</label>
                    </div>

                </div>

            <div style="line-height: 1" v-if="isInvoice==1">
                <span class="aui-font-size-14">发票抬头:</span><input type="text" placeholder="请输入发票抬头" style="flex: 1;"  v-model="invoiceClient">
                <!--</span><input type="text" placeholder="" style="flex: 1;" :disabled="isInvoice==0" disabled>-->
            </div>
            <div>
                <!--<p>{{isInvoice}}</p>-->
            </div>
        </div>

    </div>
    <div class="order-foot font-base-28">
        <span>共<span class="c-r">{{allNum}}</span>件，&nbsp;&nbsp;总金额<span class="c-r">&nbsp;&yen;&nbsp;{{splitPrice(orderView.allPrice)[0]}}.<span class="money1">{{splitPrice(orderView.allPrice)[1]}}</span></span></span>
        <a href="#none" @click.prevent="goOrder">提交订单</a>

    </div>
    <!--<p>{{add}}</p>-->
    <!--!TODO 切换地址获取运费没有做完，更换地址 -->
</div>
    <script src="../../js/aui-toast.js"></script>
    <script src="../../js/vue.min.js"></script>
    <script src="../../js/vue-resource.min.js"></script>
    <script src="../../js/common.js"></script>
    <script>
        setToken();
        new Vue({
            el:'#app',
            data:{
                orderView:{},
                freight:{},
                orderRemarks:'',
                isChecked:true,
                isInvoice:0,
                invoiceClient:''
            },
            mounted:function(){
                this.$nextTick(function () {
                    this.isCart()
                    //this.submitImmediateOrder()
                })
            },
            computed:{
                allNum:function () {
                    var t=0;
                    this.orderView.goodsList.forEach(function (v,i) {
                      //  console.log(v);
                        t+=parseFloat(v.num);
                    });
                    return t;
                }
            },
            methods:{
                //获取订单列表 是购物车遍历出来的
                submitCart:function () {
                    var _this=this;
                    _this.$http.get(SURL+'app/Carts/checkCarts').then(function (res) {
                        //console.log(res)
                        if(res.body.status==1){
                            _this.orderView=res.body.data;
                            _this.orderView.goodsList.forEach(function (v,i) {
                                _this.$set(v,'orderRemarks','');
                                var t=0,n=0 ;//初始化价格跟个数都是0
                                v.list.forEach(function (o,j) {
                                    n+=parseFloat(o.cartNum);
                                    t+=parseFloat(o.specPrice*o.cartNum);
                                });
                                // console.log(n ,t);
                                //计算出每个店铺商品的总价以及个数
                                _this.$set(v,'minMoney',t);
                                _this.$set(v,'num',n);
                            });
                            // console.log(this.orderView)
                            _this.getFreight()
                        }else{
                            showDefault('fail',res.body.msg)
                        }
                    })

                },
                //获取运费

                getFreight:function () {
                    var _this=this;
                   // console.log(_this.orderView)
                    _this.$http.get(SURL+"app/Carts/newAddressPrice",{params:{cityId:_this.orderView.address.cityId}}).then(function (res) {
                        //console.log(res)
                        if(res.body.status==1){
                            this.freight=res.body.data
                            // console.log(this.freight)
                        }

                    })
                },

                //立即购物获取订单详情//立即购买
                buyImmediately:function(){
                    // this.isAlert=!this.isAlert;
                    //this.immediateBuy=true;
                    var _this=this;
                    _this.$http.post(SURL+'app/Carts/BuyImmediately',JSON.parse(getUrlParam('params')),{emulateJSON : true}).then(function (res) {
                        if(res.body.status==1){
                        //  console.log(res)
                            _this.orderView=res.body.data
                            _this.orderView.goodsList.forEach(function (v,i) {
                                _this.$set(v,'orderRemarks','');
                                var t=0,n=0 ;//初始化价格跟个数都是0
                                 n=_this.orderView.cartNum;
                                t=_this.orderView.allPrice;
                                //计算出每个店铺商品的总价以及个数
                                _this.$set(v,'minMoney',t);
                                _this.$set(v,'num',n);
                            });
                            console.log(_this.orderView)

                        }else{
                            showDefault('fail',res.body.msg)
                        }
                    })
                },
                //判断是立即购买，还是购物车添加
                isCart:function(){
                    //type=1.立即购买
                    if(getUrlParam('type')){
                        this.buyImmediately()

                    }else{
                        this.submitCart()
                    }
                },



                //提交订单
                /*
                *
                *  "addressId": 222,用户地址ID
                    "isInvoice": 0,是否需要发票 1 是 0否 默认 0
		            "invoiceClient": 0,发票抬头  $isInvoice为1时必需
                    "orderRemarks": "",订单备注
                *
                *
                * */
                submitOrder:function () {
                    var a=[],_this=this;
                    _this.orderView.goodsList.forEach(function (v,i) {
                        var b={
                            orderRemarks:v.orderRemarks,
                            shopId:v.list[0].shopId
                        };
                        a.push(b)
                    });
                  //  console.log(a);
                    var params={
                        addressId:_this.orderView.address.addressId,
                        isInvoice:_this.isInvoice,
                        invoiceClient:_this.invoiceClient,
                        orderRemarks:JSON.stringify(a)

                    };
                    _this.$http.get(SURL+"app/Orders/h5orderPay",{params:params}).then(function (res) {
                            // console.log(res)
                        showDefault('loading','')
                        if(res.body.status==1){

                            showDefault('success','提交成功');
                            location.href="./hm-cashier2.html?list="+res.body.data.list+"&totalMoney="+_this.orderView.allPrice+'&isBatch=1'
                        }else{

                            showDefault('fail','提交失败');
                        }
                    }).catch(function (res) {
                        showDefault('fail','网络错误')
                    })
                },
                //立即购买提交订单
                submitImmediateOrder:function () {
                    var _this=this;
                    //立即购买提交订单，需要与购物车区分

                    var params={

                        cartNum: _this.orderView.cartNum,//购买数量
                            goodsId: "50",//商品ID
                    goodsSpecId: _this.orderView.goodsSpecId,//商品规格值ID
                    specIds: "",//商品规格值
                    addressId: _this.orderView.address.addressId,//地址ID
                    isInvoice:_this.isInvoice,//是否需要发票 1 是 0否 默认 0
                    invoiceClient:_this.invoiceClient,//发票抬头  $isInvoice为1时必需
                    orderRemarks: _this.orderRemarks,//订单备注
                    allAddressPrice: _this.orderView.allAddressPrice,//,中运费
                    allPrice: _this.orderView.allPrice,//总价格
                    allPayScore:_this.orderView.allPayScore,//总积分
                    shopPrice: "",//商品价格
                    shopId: "",//商品ID
                    goodsName: "",//商品名字
                    goodsImg: ""//商品图片

                    }
                    params=Object.assign(params,this.orderView.goodsList[0].list[0]);
                    console.log(params)

                    this.$http.get(SURL+'app/orders/orderPayBuy',{params:params}).then(function (res) {
                        console.log(res)
                        if(res.body.status==1){
                            location.href="./hm-cashier2.html?list="+res.body.data+"&totalMoney="+_this.orderView.allPrice+'&isBatch=0'
                        }else{
                            showDefault('fail',res.body.msg)
                        }
                    })

                },

                goOrder:function () {
                    if(getUrlParam('type')){
                        this.submitImmediateOrder()
                    }else{
                        this.submitOrder()
                    }
                }

            },

        })
    </script>
</body>
</html>
